//
//  RelatedEquipmentVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/7.
//  Copyright © 2020 tym. All rights reserved.
//

#import "RelatedEquipmentVC.h"
#import "GroupShadowTableView.h"
#import "Sizes.h"
#import "UIButton+Gradient.h"
#import "RelatedEquipmentCell.h"
#import "EquipmentService.h"
@interface RelatedEquipmentVC ()<GroupShadowTableViewDelegate,GroupShadowTableViewDataSource>

@property(nonatomic,strong)  GroupShadowTableView *tableView;

@property(nonatomic,weak) IBOutlet UIButton *configBtn;

@property(nonatomic,strong) EquipmentService *equipService;
@property(nonatomic,strong) RoomModel *roomModel;

@property(nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation RelatedEquipmentVC

-(instancetype)initWithRoomModel:(RoomModel*)roomModel{
    self = [super init];
    if (self) {
        _roomModel = roomModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"关联设备"];
    
    [self setTable];
    self.tableView.ly_emptyView = [TYMEmptyView emptyViewWithImageStr:@"img_empt_equipment"
     titleStr:@"该房间暂无可关联设备"
    detailStr:@""];
    [self.configBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
       self.configBtn.layer.cornerRadius = 5;
       self.configBtn.layer.masksToBounds = YES;
    self.configBtn.hidden = YES;
    [self getnoRoomEquipments];
}

-(NSMutableArray*)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

-(EquipmentService*)equipService{
    if (_equipService == nil) {
        _equipService = [[EquipmentService alloc] init];
    }
    return _equipService;
}

- (void)setTable {
    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight-NavigateonHeight-74-TabbarHeight-10) style:UITableViewStyleGrouped];
    self.tableView.groupShadowDelegate = self;
    self.tableView.groupShadowDataSource = self;
    self.tableView.backgroundColor = viewBGColor;
    self.tableView.sectionFooterHeight = 0;
    _tableView.showSeparator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

//获取还没有绑定房间的设备
-(void)getnoRoomEquipments{
    TYMWeak(self);
    [self.equipService getNOroomequipments:_roomModel.familyRoomId?_roomModel.familyRoomId:_roomModel.id completion:^(NSError * _Nonnull error, NSArray<EquipmentModel *> * _Nonnull array) {
        [weakself.dataArray addObjectsFromArray:array];
        [weakself.tableView reloadData];
        if(weakself.dataArray.count == 0){
            [weakself.tableView ly_showEmptyView];
            weakself.configBtn.hidden = YES;
        }else{
            weakself.configBtn.hidden = NO;
        }
    } ];
}

//确定关联
-(IBAction)doRelatedAction:(id)sender{
    NSMutableArray *seleArray = [[NSMutableArray alloc] init];
    for (EquipmentModel *equModel in self.dataArray){
        if (equModel.isSelected) {
            [seleArray addObject:[NSString stringWithFormat:@"%ld", equModel.equipmentId]];
        }
    }
    
    if (!seleArray.count) {
        [WHToast showMessage:@"未选择关联设备" duration:2 finishHandler:nil];
        return;
    }
    
    [self.equipService getMoveEquipment:seleArray familyRoomId:self.roomModel.familyRoomId?self.roomModel.familyRoomId:self.roomModel.id completion:^(LMJBaseResponse * _Nonnull response) {
        if (response.code == 0) {
            [WHToast showMessage:@"设备关联成功" duration:2 finishHandler:^{
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }else{
            [WHToast showMessage:response.message duration:2 finishHandler:nil];
        }
    }];
}

#pragma mark delegate datasource
- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [RelatedEquipmentCell cellForHeight];
}

- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RelatedEquipmentCell  *cell = [tableView dequeueReusableCellWithIdentifier:[RelatedEquipmentCell identifier]];
    if (!cell) {
        cell = [[RelatedEquipmentCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[RelatedEquipmentCell identifier]];
    }
    [cell setEquipMent:self.dataArray[indexPath.section]];
    return cell;
}

-(void)groupShadowTableView:(GroupShadowTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    EquipmentModel *model = self.dataArray[indexPath.section];
    model.isSelected = !model.isSelected;
    [self.tableView reloadData];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
